package org.dromara.quickswing.ui.app;

import raven.toast.Notifications;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * @author Jin
 */
public abstract class QSAction<T extends QSWidget<? extends QSPrefs>> extends AbstractAction {

    protected transient T app;

    protected String key;

    protected QSAction(T app) {
        this.app = app;
    }

    protected QSAction(T app, String key) {
        super(key);
        this.app = app;
        this.key = key;
    }

    protected QSAction(T app, String key, Icon icon) {
        super(key, icon);
        this.app = app;
        this.key = key;
    }

    public T getApp() {
        return app;
    }

    public String getKey() {
        if(key == null){
            return this.getClass().getName();
        }
        return key;
    }

    public KeyStroke getKeyStroke() {
        return null;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            handleAction(e);
        } catch (Exception ex) {
            Notifications.getInstance().setJFrame((JFrame) app);
            Notifications.getInstance().show(Notifications.Type.ERROR, ex.getMessage());
        }
    }

    public abstract void handleAction(ActionEvent e);
}